﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Linq;

using Gman.Service.Implementations;
using Gman.Service.Interfaces;
using Gman.Service.Message.Resquest_Response;
using Gman.Entity.Entities;
using Gman.Business;

using AutoMapper;
using System.Transactions;

namespace Gman.Service.Implementations
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class GestionService : IGestionService
    {

        #region IGestionService Members

        public string HelloWorld()
        {
            return "Hello world";
        }

        #endregion


        #region Diagnostico

        public Response_Diagnostico ListarDiagnostico(Request_Diagnostico request)
        {
            Response_Diagnostico response = new Response_Diagnostico();
            response.CorrelationId = request.RequestId;

            DiagnosticoBE DiagnosticoDTO = request.DiagnosticoDTO as DiagnosticoBE;

            if (request.LoadOptions.Contains("ListDiagnostico"))
            {
                DiagnosticoBL oDiagnosticoBL = new DiagnosticoBL();
                DiagnosticoBE oCopyBE = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(DiagnosticoDTO);

                response.LstDiagnostico = oDiagnosticoBL.Listar(oCopyBE);
            }

            return response;
        }


        public Response_Diagnostico RegistrarDiagnostico(Request_Diagnostico request)
        {
            Response_Diagnostico response = new Response_Diagnostico();
            response.CorrelationId = request.RequestId;

            DiagnosticoBE DiagnosticoDTO = request.DiagnosticoDTO as DiagnosticoBE;

            try
            {
                if (request.LoadOptions.Contains("AddDiagnostico"))
                {
                    DiagnosticoBL oDiagnosticoBL = new DiagnosticoBL();
                    DiagnosticoDetalleBL oDiagnosticoDetalleBL = new DiagnosticoDetalleBL();
                    DiagnosticoBE oCopyBE = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(DiagnosticoDTO);

                    using (TransactionScope trans = new TransactionScope())
                    {

                        response.InsDiagnostico = oDiagnosticoBL.Registrar(request.DiagnosticoDTO);

                        if (request.listDiagnosticoDetalleBE != null)
                        {
                            foreach (var diagnosticoDetalle in request.listDiagnosticoDetalleBE)
                            {
                                diagnosticoDetalle.IDDiagnostico = response.InsDiagnostico;
                                oDiagnosticoDetalleBL.Registrar(diagnosticoDetalle);
                            }
                        }
                        trans.Complete();
                    }
                }

            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }

            return response;
        }


        public Response_Diagnostico ObtenerDiagnostico(Request_Diagnostico request)
        {
            Response_Diagnostico response = new Response_Diagnostico();
            response.CorrelationId = request.RequestId;

            DiagnosticoBE DiagnosticoDTO = request.DiagnosticoDTO as DiagnosticoBE;

            if (request.LoadOptions.Contains("GetDiagnostico"))
            {
                DiagnosticoBL oDiagnosticoBL = new DiagnosticoBL();
                DiagnosticoBE oCopyBE = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(DiagnosticoDTO);

                response.GetDiagnostico = oDiagnosticoBL.Obtener(oCopyBE);
            }

            return response;
        }


        public Response_Diagnostico ActualizarDiagnostico(Request_Diagnostico request)
        {

            Response_Diagnostico response = new Response_Diagnostico();
            response.CorrelationId = request.RequestId;

            DiagnosticoBE DiagnosticoDTO = request.DiagnosticoDTO as DiagnosticoBE;

            try
            {
                if (request.LoadOptions.Contains("UpdDiagnostico"))
                {
                    DiagnosticoBL oDiagnosticoBL = new DiagnosticoBL();
                    DiagnosticoDetalleBL oDiagnosticoDetalleBL = new DiagnosticoDetalleBL();
                    DiagnosticoBE oCopyBE = Mapper.Map<DiagnosticoBE, DiagnosticoBE>(DiagnosticoDTO);


                    using (TransactionScope trans = new TransactionScope())
                    {

                        response.UpdDiagnostico = oDiagnosticoBL.Actualizar(request.DiagnosticoDTO);

                        var IDDiagnostico = request.DiagnosticoDTO.IDDiagnostico;

                        if (request.listDiagnosticoDetalleBE != null)
                        {
                            foreach (var diagnosticoDetalle in request.listDiagnosticoDetalleBE)
                            {
                                if (diagnosticoDetalle.IDDiagnostico > 0)
                                {

                                    oDiagnosticoDetalleBL.Actualizar(diagnosticoDetalle);
                                }
                                else if (diagnosticoDetalle.IDDiagnostico <= 0)
                                {
                                    diagnosticoDetalle.IDDiagnostico = IDDiagnostico;
                                    oDiagnosticoDetalleBL.Registrar(diagnosticoDetalle);
                                }

                            }
                        }

                        if (request.toDelete != null)
                        {
                            foreach (var eliminados in request.toDelete)
                            {
                                oDiagnosticoDetalleBL.Eliminar(eliminados);
                            }

                        }

                        trans.Complete();
                    }
                }

            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }

            return response;

        }


        #endregion


        #region DiagnosticoDetalle


        public Response_DiagnosticoDetalle ListarDiagnosticoDetalle(Request_DiagnosticoDetalle request)
        {
            Response_DiagnosticoDetalle response = new Response_DiagnosticoDetalle();
            response.CorrelationId = request.RequestId;

            DiagnosticoDetalleBE DiagnosticoDetalleDTO = request.DiagnosticoDetalleDTO as DiagnosticoDetalleBE;

            if (request.LoadOptions.Contains("ListDiagnosticoDetalle"))
            {
                DiagnosticoDetalleBL oDiagnosticoDetalleBL = new DiagnosticoDetalleBL();
                DiagnosticoDetalleBE oCopyBE = Mapper.Map<DiagnosticoDetalleBE, DiagnosticoDetalleBE>(DiagnosticoDetalleDTO);

                response.LstDiagnosticoDetalle = oDiagnosticoDetalleBL.Listar(oCopyBE);
            }

            return response;
        }


        #endregion




        #region Ot

        public Response_OrdenTrabajo ObtenerOTxNro(Request_OrdenTrabajo Request)
        {
            Response_OrdenTrabajo response = new Response_OrdenTrabajo();


            if (Request.LoadOptions.Contains("ObtenerOTxNro"))
            {
                OrdenTrabajoBL oOrdenTrabajoBL = new OrdenTrabajoBL();
                response.GetOrdenTrabajo = oOrdenTrabajoBL.ObtenerXNro(Request.OrdenTrabajoDTO);
            }
            return response;
        }


        #endregion


        #region asnc

        public Response_ActaServNoConforme RegistrarAsnc(Request_ActaServNoConforme request)
        {
            Response_ActaServNoConforme response = new Response_ActaServNoConforme();
            try
            {
                if (request.LoadOptions.Contains("Add"))
                {
                    ActaServNoConformeBL oAsncBL = new ActaServNoConformeBL();
                    using (TransactionScope trans = new TransactionScope())
                    {
                        response.InsActaServNoConforme = oAsncBL.Registrar(request.ActaServNoConformeDTO);
                        trans.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }

            return response;
        }



        public Response_ActaServNoConforme ActualizarAsnc(Request_ActaServNoConforme request)
        {
            Response_ActaServNoConforme response = new Response_ActaServNoConforme();
            try
            {
                if (request.LoadOptions.Contains("update"))
                {
                    ActaServNoConformeBL oAsncBL = new ActaServNoConformeBL();
                    using (TransactionScope trans = new TransactionScope())
                    {
                        response.UpdActaServNoConforme = oAsncBL.Actualizar(request.ActaServNoConformeDTO);
                        trans.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }

            return response;
        }



        public Response_ActaServNoConforme ObtenerXNroAsnc(Request_ActaServNoConforme request)
        {
            Response_ActaServNoConforme response = new Response_ActaServNoConforme();

            if (request.LoadOptions.Contains("obtenerAsnc"))
            {

                ActaServNoConformeBL oAsncBL = new ActaServNoConformeBL();

                response.GetActaServNoConforme = oAsncBL.ObtenerXNroAsnc(request.ActaServNoConformeDTO);


            }
            return response;
        }

        #endregion





    }
}
